package com.jeesite.modules.common;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * 生成和解析二维码
 * @author oddbitter
 *
 */
public class QRCodeUtils {  

	private static final Logger logger = LoggerFactory.getLogger(QRCodeUtils.class);
    public static void main(String[] args) throws Exception {  
//         encode("我正在测试");  
        decode("D:/zxing.png");  
    }


    /**
     * 生成二维码
     *
     * @throws WriterException
     * @throws IOException
     */
    public static String encodeLocal(String filePath,String content) throws WriterException, IOException {
//        String filePath = System.getProperty("java.io.tmpdir");
        String fileName = UUID.randomUUID()+".png";
//        String content = "测试zxing生成二维码";
        int width = 300; // 图像宽度
        int height = 300; // 图像高度
        String format = "png";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
        Path path = FileSystems.getDefault().getPath(filePath, fileName);
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
        System.out.println("输出成功.");

        return fileName;
    }


    /** 
     * 生成二维码 
     *  
     * @throws WriterException
     * @throws IOException 
     */  
    public static String encode(String content) throws WriterException, IOException {
        String filePath = System.getProperty("java.io.tmpdir");
//        String filePath = "/Users/qing/Desktop/";
        String fileName = UUID.randomUUID()+".png";
//        String content = "测试zxing生成二维码";  
        int width = 115; // 图像宽度
//        int width = 160; // 图像宽度
        int height = 115; // 图像高度
//        int height = 160; // 图像高度
        String format = "png";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
        Path path = FileSystems.getDefault().getPath(filePath, fileName);  
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
        System.out.println("输出成功.");  
        
        
        
		String picUrl = QiniuUtils.upload(path.toFile(), fileName);
		logger.info("picUrl={}",picUrl);
		return picUrl;
    }
    /**
     * 生成二维码
     *
     * @throws WriterException
     * @throws IOException
     */
    public static String encodeIso(String content) throws WriterException, IOException {
        String filePath = System.getProperty("java.io.tmpdir");
        String fileName = UUID.randomUUID()+".png";
//        String content = "测试zxing生成二维码";
        int width = 300; // 图像宽度
        int height = 300; // 图像高度
        String format = "png";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
        Path path = FileSystems.getDefault().getPath(filePath, fileName);
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
        System.out.println("输出成功.");



        String picUrl = QiniuUtils.upload(path.toFile(), fileName);
        logger.info("picUrl={}",picUrl);
        return picUrl;
    }

    /** 
     * 解析二维码 
     */  
    public static String decode(String filePath) {  
//        String filePath = "D://zxing.png";  
        BufferedImage image;  
        try {  
            image = ImageIO.read(new File(filePath));  
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
            logger.info("图片中内容：  {}",result.getText());  
            return result.getText();
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (NotFoundException e) {
            e.printStackTrace();  
        }  
        return null;
    }  
    public static String decode(File file) {  
//        String filePath = "D://zxing.png";  
    	BufferedImage image;  
    	try {  
    		image = ImageIO.read(file);  
    		LuminanceSource source = new BufferedImageLuminanceSource(image);
    		Binarizer binarizer = new HybridBinarizer(source);
    		BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
    		Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
    		hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
    		Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
    		logger.info("图片中内容：  {}",result.getText());  
    		return result.getText();
    	} catch (IOException e) {  
    		e.printStackTrace();  
    	} catch (NotFoundException e) {
    		e.printStackTrace();  
    	}  
    	return null;
    }


  
}  